package com.jiadakeji.ppm.api.v1;


import com.jiadakeji.ppm.api.common.JsonResult;
import com.jiadakeji.ppm.common.utils.StringUtils;
import com.jiadakeji.ppm.common.web.BaseController;
import com.jiadakeji.ppm.modules.business.dao.RoomDao;
import com.jiadakeji.ppm.modules.business.entity.AskRoom;
import com.jiadakeji.ppm.modules.business.entity.Room;
import com.jiadakeji.ppm.modules.business.service.AskRoomService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController("v1_askRoomController")
@RequestMapping(value = "${apiPath}/v1/askRoom")
public class AskRoomController extends BaseController {

    @Autowired
    private AskRoomService askRoomService;
    @Autowired
    private RoomDao roomDao;

    @RequestMapping(value = "/insert")
    @ApiOperation(value = "获取小区列表",httpMethod = "POST",consumes="application/json",notes="根据id")
    public JsonResult<String> list(@RequestBody AskRoom askRoom, HttpServletRequest request, HttpServletResponse response){
        JsonResult<String> result = new JsonResult<>();
        if(StringUtils.isBlank(askRoom.getCustomerId())) {
            return result.setCodeAndMessage(-1, "customerId 不能为空");
        }
        if(StringUtils.isBlank(askRoom.getRoomId())){
            return result.setCodeAndMessage(-1,"roomId 不能为空");
        }
        Room room = roomDao.get(askRoom.getRoomId());
        if(null != room.getHouseHolderId() &&room.getHouseHolderId().equals(askRoom.getCustomerId())){
            return result.setCodeAndMessage(-2,"此房已经是你的房了");
        }
        askRoom.setStatus("0");
        askRoom.setCreateDate(new Date(System.currentTimeMillis()));
        askRoomService.save(askRoom);
        return result;
    }
}
